package com.gpzx.zx.module.home.frame.view

import android.text.TextUtils
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.gpzx.zx.R
import com.gpzx.zx.module.home.ui.activity.InfoWebViewActivity
import com.gpzx.zx.module.home.ui.adapter.NewsCommentAdapter
import com.qmuiteam.qmui.widget.QMUITopBar
import com.wkq.base.frame.mosby.delegate.MvpView
import com.wkq.database.utils.DataBaseUtils
import com.wu.common.utils.AlertUtil
import com.wu.common.utils.StatusBarUtil
import com.zzhoujay.richtext.RichText

/**
 *
 * 作者:吴奎庆
 *
 * 时间:2020/6/12
 *
 * 用途:
 */


class InfoWebView : MvpView, View.OnClickListener {

    private val anim = intArrayOf(R.anim.slide_bottom_in, 0, 0, R.anim.slide_bottom_out)
    var mActivity: InfoWebViewActivity
    var newsCommentAdapter: NewsCommentAdapter? = null
    var isCollection:Boolean?=false

    constructor(mActivity: InfoWebViewActivity) {
        this.mActivity = mActivity
    }

    fun initView() {
        initToolBar(mActivity.title)
        mActivity.binding.onclick = this

    }

    fun initToolBar(title: String?) {
        StatusBarUtil.setStatusBarDarkMode(mActivity)
        StatusBarUtil.setColor(mActivity, mActivity!!.getResources().getColor(R.color.color_2b2b2b), 0)
        var toolbar = mActivity!!.binding.includeToolbar as QMUITopBar

        toolbar.setTitle(title)
        toolbar.setBackgroundColor(mActivity!!.resources.getColor(R.color.color_2b2b2b))
        toolbar.addLeftImageButton(R.drawable.ic_arrow_back_white_24dp, R.id.qmui_topbar_item_left_back).setOnClickListener { mActivity!!.finish() }
    }


    fun showView(icon: String?, desc: String?, newsTitle: String?, newsAuthorName: String?, newsAuthorIcon: String?, newsAuthorDesc: String?, id: String?, newsId: String?, newsType: String?) {
        var strs: String? = null
        if (TextUtils.isEmpty(icon)) {
            strs = ""
        } else {
            strs = "<img src=" + icon + ">" + "</'img'>"
        }
        var newStr = desc!!.replace("\n", "<br/>").toString()
        var strP = "<p>" + newStr + "</p>"
        val html = "<html><body>" + strs + strP + "</body></html>"
        RichText.fromHtml(html).into(mActivity.binding.etNewContent);

        mActivity.newsDesc=desc
        mActivity.newsIcon=icon


        if (TextUtils.isEmpty(newsAuthorIcon)) {
            mActivity.binding.card.visibility = View.GONE
        } else {
            Glide.with(mActivity).load(newsAuthorIcon).into(mActivity.binding.ivIcon)
        }
        if (TextUtils.isEmpty(newsAuthorName)) {
            mActivity.binding.tvName.visibility = View.GONE
        } else {
            mActivity.binding.tvName.visibility = View.VISIBLE
            mActivity.binding.tvName.setText(newsAuthorName)
            mActivity.binding.tvDesc.setText("暂无")
        }
        if (TextUtils.isEmpty(newsAuthorDesc)) {
            mActivity.binding.tvName.visibility = View.GONE
        } else {
            mActivity.binding.tvName.visibility = View.VISIBLE
            mActivity.binding.tvDesc.setText(newsAuthorDesc)
        }
        if (TextUtils.isEmpty(newsTitle)) {
            mActivity.binding.tvName.visibility = View.GONE
        } else {
            mActivity.binding.tvName.visibility = View.VISIBLE
            mActivity.binding.tvTitle.setText(newsTitle)
        }

        isCollection = showCollection(newsId)

        mActivity.binding.ivCollection.setOnClickListener {
            if (isCollection == true) {
                DataBaseUtils.cancelCollection(mActivity, newsId);
                isCollection = showCollection(newsId)
            } else {
                DataBaseUtils.collection(mActivity, id, newsId, newsType, System.currentTimeMillis().toString(), newsTitle, icon, newsAuthorName, desc, mActivity.type);
                isCollection = showCollection(newsId)
            }

        }
        var commentList = DataBaseUtils.getComments(mActivity, mActivity.index, mActivity.type);

        newsCommentAdapter = NewsCommentAdapter(mActivity, R.layout.item_news_commnent)
        mActivity.binding.rvCommont.layoutManager = LinearLayoutManager(mActivity)
        mActivity.binding.rvCommont.adapter = newsCommentAdapter
        newsCommentAdapter!!.addItems(commentList)

        if (commentList.size > 0) {
            mActivity.binding.rvCommont.visibility = View.VISIBLE
        } else {
            mActivity.binding.rvCommont.visibility = View.GONE
            mActivity.binding.ivCommentEmpty.visibility = View.VISIBLE
        }


    }

    private fun showCollection(newsId: String?): Boolean {
        var isCollection = DataBaseUtils.isCollection(mActivity, newsId);
        if (isCollection == true) {
            Glide.with(mActivity).load(R.drawable.iv_collection).into(mActivity.binding.ivCollection)
        } else {
            Glide.with(mActivity).load(R.drawable.iv_un_collection).into(mActivity.binding.ivCollection)
        }
        return isCollection
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.iv_comment_empty -> {
                comment()
            }
            R.id.r_comment -> {
                comment()
            }
        }
    }

    fun comment() {

        val detailFragment: Fragment = ARouter.getInstance()
                .build("/topic/TopicInputFragment")
                .withBoolean("showEmoji", false)
                .withString("news_type", mActivity.type)
                .withString("news_Id", mActivity.index)
                .withString("news_title", mActivity.title)
                .withString("news_icon", mActivity.newsIcon)
                .withString("news_desc", mActivity.newsDesc)


                .navigation(mActivity) as Fragment
        if (null != detailFragment) {
            if (null != mActivity.supportFragmentManager && null != mActivity.supportFragmentManager.beginTransaction()) {

                mActivity.supportFragmentManager.beginTransaction().setCustomAnimations(anim.get(0), anim.get(1), anim.get(2), anim.get(3))
                        .add(R.id.fragment_comment, detailFragment).addToBackStack(null).commitAllowingStateLoss()

            }
        }
    }


    fun showMessage(message: String) {
        if (TextUtils.isEmpty(message)) return
        AlertUtil.showDeftToast(mActivity, message)
    }


}